package com.ruoyi.web.controller.assets;

import com.ruoyi.assets.domain.StateAsset;
import com.ruoyi.assets.domain.dto.StateAssetQueryDto;
import com.ruoyi.assets.service.IStateAssetService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Arrays;
import java.util.List;

/**
 * 资产Controller
 * 
 * @author ruoyi
 */
@RestController
@RequestMapping("/assets/asset")
public class StateAssetController extends BaseController {

    @Autowired
    private IStateAssetService stateAssetService;

    /**
     * 查询资产列表
     */
    @PreAuthorize("@ss.hasPermi('assets:asset:list')")
    @GetMapping("/list")
    public TableDataInfo<StateAsset> list(StateAssetQueryDto stateAssetQuery) {
        startPage();
        List<StateAsset> list = stateAssetService.selectStateAssetList(stateAssetQuery);
        return getDataTable(list);
    }

    /**
     * 导出资产列表
     */
    @PreAuthorize("@ss.hasPermi('assets:asset:export')")
    @Log(title = "资产", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, StateAssetQueryDto stateAssetQuery) {
        stateAssetService.exportStateAsset(response, stateAssetQuery);
    }

    /**
     * 获取资产详细信息
     */
    @PreAuthorize("@ss.hasPermi('assets:asset:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(stateAssetService.selectStateAssetById(id));
    }

    /**
     * 新增资产
     * 新增时会自动处理街道信息
     */
    @PreAuthorize("@ss.hasPermi('assets:asset:add')")
    @Log(title = "资产", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody StateAsset stateAsset) {
        return toAjax(stateAssetService.insertStateAsset(stateAsset));
    }

    /**
     * 修改资产
     * 修改时会自动处理街道信息
     */
    @PreAuthorize("@ss.hasPermi('assets:asset:edit')")
    @Log(title = "资产", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody StateAsset stateAsset) {
        return toAjax(stateAssetService.updateStateAsset(stateAsset));
    }

    /**
     * 删除资产
     */
    @PreAuthorize("@ss.hasPermi('assets:asset:remove')")
    @Log(title = "资产", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(stateAssetService.deleteStateAssetByIds(Arrays.asList(ids)));
    }
    
    /**
     * 批量审核资产
     */
    @PreAuthorize("@ss.hasPermi('assets:asset:audit')")
    @Log(title = "资产审核", businessType = BusinessType.UPDATE)
    @PutMapping("/audit/{auditStatus}")
    public AjaxResult audit(@RequestBody Long[] ids, @PathVariable("auditStatus") Integer auditStatus) {
        List<Long> idList = Arrays.asList(ids);
        return toAjax(stateAssetService.batchAuditStateAsset(idList, auditStatus));
    }
    
    /**
     * 导入资产数据
     */
    @PreAuthorize("@ss.hasPermi('assets:asset:import')")
    @Log(title = "资产导入", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception {
        String message = stateAssetService.importStateAsset(file, updateSupport);
        return success(message);
    }

    /**
     * 获取资产导入模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<StateAsset> util = new ExcelUtil<>(StateAsset.class);
        util.importTemplateExcel(response, "资产数据");
    }
} 